<template>
<van-button
  class="follow-btn"
  type="info"
  color="#3296fa"
  round
  size="small"
  icon="plus"
  v-if="!isFollowed"
  @click="onFollowed"
  :loading="isFollowedLoading"
>关注</van-button>
<van-button
  :loading="isFollowedLoading"
  @click="onFollowed"
  v-else
  class="follow-btn"
  round
  size="small"
>已关注</van-button>
</template>

<script>
import { deleteUser, followedUser } from '@/api/user.js'
export default {
  data () {
    return {
      isFollowedLoading: false
    }
  },
  props: {
    isFollowed: {
      type: Boolean,
      required: true
    },
    autId: {
      type: [Number, String, Object],
      required: true
    }
  },
  created () {

  },

  // 用来进行v-model的自定义解析
  model: {
    prop: 'isFollowed',
    event: 'upDataFollowed'
  },

  methods: {
    // 点击是否关注
    async onFollowed () {
      this.isFollowedLoading = true
      try {
        if (this.isFollowed) {
          await deleteUser(this.autId)
        } else {
          await followedUser(this.autId)
        }
        this.$emit('upDataFollowed', !this.isFollowed)
        this.isFollowedLoading = false
      } catch (err) {
        // 失败了
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
